package com.buka.util;

import com.google.gson.Gson;
import com.qiniu.common.QiniuException;
import com.qiniu.http.Response;
import com.qiniu.storage.Configuration;
import com.qiniu.storage.Region;
import com.qiniu.storage.UploadManager;
import com.qiniu.storage.model.DefaultPutRet;
import com.qiniu.util.Auth;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.util.HashSet;
import java.util.UUID;

public class QiuOS {

    public static void main3(String[] args) throws Exception {
        //        身份验证
        String ak = "RD8HE1GTuwoj4AZbL1B35zsXgGGLUqRvf8RzMDka";
        String sk = "bzApVfWuRKOTG0k-2DNRRLLArNAPLTW-fz3z1ngS";
        String bucket = "buka-jcx";
        Auth auth = Auth.create(ak, sk);
        String upToken = auth.uploadToken(bucket);

        //配置对象
        //构造一个带指定 Region 对象的配置类  // 区域名称：z0 华东  z1 华北  z2 华南  na0 北美  as0 东南亚

        Configuration cfg = Configuration.create(Region.createWithRegionId("z1"));
        cfg.resumableUploadAPIVersion = Configuration.ResumableUploadAPIVersion.V2;

        UploadManager uploadManager = new UploadManager(cfg);
        String imagePath = "D:\\images\\图标\\持续学习.jpg";

        File file = new File(imagePath);
        FileInputStream fileInputStream = new FileInputStream(file);
        byte[] bytes = new byte[(int) file.length()];
        fileInputStream.read(bytes);




        //key 如果为空  默认使用的是文件的hash值
        Response put = uploadManager.put(bytes, null, upToken);
        DefaultPutRet defaultPutRet = new Gson().fromJson(put.bodyString(), DefaultPutRet.class);
        System.out.println(defaultPutRet.key);
        System.out.println(defaultPutRet.hash);

    }

    public static void main2(String[] args) throws QiniuException {

//        身份验证
        String ak = "RD8HE1GTuwoj4AZbL1B35zsXgGGLUqRvf8RzMDka";
        String sk = "bzApVfWuRKOTG0k-2DNRRLLArNAPLTW-fz3z1ngS";
        String bucket = "buka-jcx";
        Auth auth = Auth.create(ak, sk);
        String upToken = auth.uploadToken(bucket);

        //配置对象
        //构造一个带指定 Region 对象的配置类  // 区域名称：z0 华东  z1 华北  z2 华南  na0 北美  as0 东南亚

        Configuration cfg = Configuration.create(Region.createWithRegionId("z1"));
        cfg.resumableUploadAPIVersion = Configuration.ResumableUploadAPIVersion.V2;

        UploadManager uploadManager = new UploadManager(cfg);
        String imagePath = "D:\\images\\图标\\持续学习.jpg";
        //key 如果为空  默认使用的是文件的hash值
        Response put = uploadManager.put(imagePath, null, upToken);
        DefaultPutRet defaultPutRet = new Gson().fromJson(put.bodyString(), DefaultPutRet.class);
        System.out.println(defaultPutRet.key);
        System.out.println(defaultPutRet.hash);


    }




}
